<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Users;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UserController extends Controller
{

    /**
     * 登录用户详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(){
        try {
            $user = auth()->guard('api')->user();
            return $this->returnJson(0,'',$user);
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }
    }

    /**
     * 分类管理_用户列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function all(){
        try {
            $list = Users::where('is_admin',1)->get(['id','username']);
            return $this->returnJson(0, '',$list);
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }

    }

    /**
     * 用户管理
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){
        try {
            $params = [
                'username'    => (string)request('username',''),
            ];
            $lists  = Users::where(function($query) use($params){
                    if ($params['username']){
                        $query->where('username','like','%'.$params['username'].'%')
                            ->orWhere('openid','like','%'.$params['username'].'%');
                    }
                })
                ->orderBy('register_at','DESC')
                ->where('is_admin',Users::ADMIN_0)
                ->paginate(10);
            return $this->returnJson(0, '',$lists);
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }

    }

    /**
     * 管理人员
     * @return \Illuminate\Http\JsonResponse
     */
    public function manage(){
        try {
            $lists  = Users::orderBy('id','ASC')
                ->where('is_admin',Users::ADMIN_1)
                ->get();
            return $this->returnJson(0, '',$lists);
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }

    }

    public function update(){
        try {
            $data = request()->all();
            if($data['id']){
                $info = Users::find($data['id']);
                if(!$info){
                    return $this->returnJson(1,'数据不存在');
                }
                if($data['password']){
                    $data['password'] = bcrypt($data['password']);
                }else{
                    unset($data['password']);
                }
                if($info->update($data)){
                    return $this->returnJson(0,'修改用户成功');
                }
                return $this->returnJson(1,'修改用户失败');
            }else{
                $user = Users::where('openid',$data['openid'])->first();
                if($user){
                    return $this->returnJson(1,'该账号已经存在');
                }
                if(empty($data['password'])) $data['password'] = '123456';
                $result = Users::register($data);
                if($result){
                    return $this->returnJson(0,'添加用户成功');
                }
                return $this->returnJson(1,'添加用户失败');
            }
        } catch (\Exception $e) {
            return $this->returnJson(1, '网络超时，请刷新再试');
        }

    }




    /**
     * 删除用户
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function destroy(){
        try {
            $id = (int)request('id');
            Users::destroy($id);
            return $this->returnJson(0, '删除成功');
        } catch (\Exception $e) {
            return $this->returnJson(1, '删除失败');
        }
    }

}